package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.NoAdoptAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.AdoptMdoel;
import com.banshuibao.bsb_android.model.ConDetailsModel;
import com.banshuibao.bsb_android.model.ConsutationModel;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.TimeUtil;
import com.banshuibao.bsb_android.view.MyListView;

import java.util.List;

public class NoAdoptActivity extends BaseActivity implements View.OnClickListener {

    private TextView content_one;
    private TextView date_one;
    private ImageView image_one, image_two, image_three;
    private String[] split;
    private MyListView myListView;
    private NoAdoptAdapter adapter;
    private TextView aStatus;
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_no_adopt);
        initView();
        getData();
    }

    private void initView() {
        setTitleText("快速咨询详情");
        content_one = (TextView) findViewById(R.id.content);
        date_one = (TextView) findViewById(R.id.date);
        image_one = (ImageView) findViewById(R.id.image_one);
        image_two = (ImageView) findViewById(R.id.image_two);
        image_three = (ImageView) findViewById(R.id.image_three);
        linearLayout = (LinearLayout) findViewById(R.id.liner);
        myListView = (MyListView) findViewById(R.id.no_adopt_list);
        aStatus = (TextView) findViewById(R.id.status);
        myListView.setDivider(null);
        image_one.setOnClickListener(this);
        image_two.setOnClickListener(this);
        image_three.setOnClickListener(this);
    }

    private void getData() {
        ConsutationModel.DataBean item = (ConsutationModel.DataBean) getIntent().getSerializableExtra("item");
        if (item != null) {
            String status = item.getStatus();
            if (!TextUtils.isEmpty(status) && status.equals("2")){
                aStatus.setVisibility(View.VISIBLE);
                myListView.setVisibility(View.GONE);
            }
            String uuid = item.getUuid();
            getDetailsPost(uuid);

            String content = item.getContent();
            String imgs = item.getImgs();
            long createDate = item.getCreateDate();

            if (!TextUtils.isEmpty(content)) {
                content_one.setText(content);
            }
            String time = null;
            if (createDate != 0) {
                time = TimeUtil.format(createDate);
            } else {
                time = "";
            }
            date_one.setText(time);
            if (!TextUtils.isEmpty(imgs)) {
                split = imgs.split(",");
                if (split.length > 0) {
                    if ("null".equals(split[0]))
                        return;
                    if (split.length == 1) {
                        ImageUtil.loadImage(this, split[0],image_one);
                        image_two.setVisibility(View.GONE);
                        image_three.setVisibility(View.GONE);
                    } else if (split.length == 2) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        ImageUtil.loadImage(this, split[1], image_two);
                        image_three.setVisibility(View.GONE);
                    } else if (split.length == 3) {
                        ImageUtil.loadImage(this, split[0], image_one);
                        ImageUtil.loadImage(this, split[1], image_two);
                        ImageUtil.loadImage(this, split[2], image_three);
                    }
                }
            } else {
                image_one.setVisibility(View.GONE);
                image_two.setVisibility(View.GONE);
                image_three.setVisibility(View.GONE);
                linearLayout.setVisibility(View.GONE);
            }
        }

    }

    private void getDetailsPost(String uuid) {
        ConDetailsModel.body body = new ConDetailsModel.body();
        body.setUuid(uuid);
        HttpUtil.doPost(Constants.Url.ASK_RAPID_DETAILS, body, new HttpResponse(this, AdoptMdoel.class) {
            @Override
            public void onResult(Object obj) {
                AdoptMdoel s = (AdoptMdoel) obj;
//                LogUtil.e(s);
                List<AdoptMdoel.DataBean> data = s.getData();
                adapter = new NoAdoptAdapter(NoAdoptActivity.this, data, R.layout.item_no_adopt);
                myListView.setAdapter(adapter);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_one:
                IntentManager.goBitmapShowActivity(NoAdoptActivity.this, split, 0);
                break;
            case R.id.image_two:
                IntentManager.goBitmapShowActivity(NoAdoptActivity.this, split, 1);
                break;
            case R.id.image_three:
                IntentManager.goBitmapShowActivity(NoAdoptActivity.this, split, 2);
                break;
        }
    }
}

